const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
//const taskService = require("../../../apiservice/task/taskservice");
const screenCallbackService = require("../../../apiservice/screen/screencallbackservice");
//const jsonData = require("./jsondata.js");

module.exports = Behavior({
	behaviors: [],
	properties: {},
	data: {},
	methods: {
		//修改筛查回访信息
		updateCallBackResult: function() {
			try {
				var screenCallBackData = {};
        

				let taskInfo = this.data.taskDetail;
				screenCallBackData.new_screencallbackid = this.data.screencallbackid;
				screenCallBackData.new_contactresult = this.data.telResult[this.data.telResultIndex].id;
				screenCallBackData.new_communicateresult = this.data.connectResult[this.data.connectResultIndex].id;
				screenCallBackData.new_description = this.data.description;
				screenCallBackData.new_task = taskInfo.taskId;
        
        
        

        screenCallBackData.reservation = reservation;
					screenCallbackService.updateScreenCallBacksData(screenCallBackData).then(function(result) {
						console.log(result);
						wx.switchTab({
							url: "../list/list",
							success: function(e) {
								var page = getCurrentPages().pop();
								if (!page) return;
								page.onLoad();
							}
						});
					}).catch(function(error) {
						console.log(error);
					})
			} catch (e) {
				console.log(e);
				//TODO handle the exception
			} finally {
				wx.hideLoading();
			}
		},

		//创建筛查回访信息
		createCallBackResult: function() {
			try {
        wx.showToast({
          title: '提交中...',
          mask:true,
          icon:"loading"
        })
				console.log(this.data.taskDetail);

				var taskInfo = this.data.taskDetail;

        var reservation = {
          new_reservationdate: this.data.appointmenttothehospitaldate,
          new_hospital: this.data.hospitalList[this.data.hospitalindex].siteId,
          new_contnt: this.data.itemData[this.data.itemindex].id,
          new_lead: taskInfo.new_leadId,
          new_mobile: taskInfo.new_mobile,
          new_remark: this.data.description
        };
        if (this.data.officeArray.length>0){
          reservation.new_department=this.data.officeArray[this.data.officeIndex].new_departmentId
        }

				var backCallResult = {
					new_lead: taskInfo.new_leadId,
					new_screen: taskInfo.new_screenId,
					new_task: taskInfo.taskId,
					new_propaganda: taskInfo.new_propaganda,
					new_contactresult: this.data.selectTelResult,
					new_communicateresult: this.data.selectConnectResult,
					new_description: this.data.description,
          reservation: reservation
				};


				screenCallbackService.createCallBackResult(backCallResult).then(function(result) {
					console.log(result);
					wx.switchTab({
						url: "../list/list",
						success: function(e) {
							var page = getCurrentPages().pop();
							if (!page) return;
							page.onLoad();
						}
					});
				}).catch(function(error) {
					console.log(error);
				})
			} catch (e) {
				console.log(e);
				//TODO handle the exception
			} finally {
				wx.hideLoading();
			}
		},

		//延迟筛查回访信息
		delayCallBackResult: function() {
			try {
				//debugger;
				var that = this;
				var screenCallBackData = {};
				var taskInfo = this.data.taskDetail
				screenCallBackData.new_screencallbackid = this.data.screencallbackid;
				screenCallBackData.new_contactresult = this.data.telResult[this.data.telResultIndex].id;
				// screenCallBackData.new_communicateresult = this.data.connectResult[this.data.connectResultIndex].id;
				screenCallBackData.new_description = this.data.description;
				screenCallBackData.new_task = taskInfo.taskId,
					screenCallbackService.UpdateScreenCallBacksDelay(screenCallBackData, this.data.delayResultId)
					.then(function(result) {
						if (result.data.resultState != 1) {
							wx.showToast({
								title: '失败',
								icon: 'success',
								duration: 2000
							})
						}
						//debugger;
						let options = {}
						options.taskid = taskInfo.taskId;

						if (that.data.delayResultId > 0) {
							wx.showToast({
								title: '成功',
								icon: 'success',
								duration: 1500,
								mask:true,
								success: function() {
									that.onLoad(options);
                                    wx.switchTab({
                                    	url: "../list/list",						
                                    });
								}
							})
						} else {
							wx.switchTab({
								url: "../list/list",
								// success: function(e) {
								// 	var page = getCurrentPages().pop();
								// 	if (!page) return;
								// 	page.onLoad();
								// }
							});
						}

					}).catch(function(error) {
						console.log(error);
					})
			} catch (e) {
				console.log(e);
				//TODO handle the exception
			} finally {
				wx.hideLoading();
			}
		},
		//初始页面数据,根据任务ID与筛查ID获取筛查回访信息
		async GetScreenCallBackData(taskId, screenId) {
			try {
				if (taskId != null && screenId != null) {
					
					console.log(screenId);
					var screenresult = await screenCallbackService.getScreenCallBackResult(taskId, screenId);
					if (!screenresult.data) return;
					//console.log(screenresult.data);

					this.getPickerValue('telResult', screenresult, 'new_contactresult',
						'telResultIndex', 'selectTelText');

					this.delayCallBack(screenresult.data["new_contactresult"]);
					console.log(screenresult.data["new_contactresult"]);

					this.getPickerValue('connectResult', screenresult, 'new_communicateresult',
						'connectResultIndex', 'selectConnectText');

					this.setData({
						screencallbackid: screenresult.data.new_screencallbackid,
						// description: screenresult.data.new_description,
					});
				}
			} catch (e) {
				console.log(e);
				//TODO handle the exception
			} finally {
				wx.hideLoading();
			}
		},
	}
})
